package com.dream.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dream.able.BackstageRecordMapper;
import com.dream.datasource.DBContextHolder;
import com.dream.pojo.BackstageRecord;
import com.dream.pojo.BackstageRecordExample;
import com.dream.service.able.All_Service_able;
import com.dream.service.able.Limit_able;

/*
 * 后台记录的业务类
 */
@Service("Backstage_Record_Serviceimpl")
public class Backstage_Record_Serviceimpl implements 
All_Service_able<BackstageRecord>,
Limit_able<BackstageRecord>{


	/*
	 * 后台记录DAO代理对象
	 */
	@Resource(name="backstageRecordMapper")
	private BackstageRecordMapper backstage_record_mapper;

	/*
	 * (添加一条后台记录，传入，后台记录内容，)
	 * @see com.dream.service.able.All_Service_able#insert(java.lang.String[])
	 */
	@Override
	public BackstageRecord insert(String... str) {
		//切换数据源,主库
		DBContextHolder.setDbType(DBContextHolder.master);
		//创建后台记录对象
		BackstageRecord backstageRecord = new BackstageRecord();
		//插入时间
		backstageRecord.setTime(new Date());
		//插入内容
		backstageRecord.setContext(str[0]);
		//插入状态
		backstageRecord.setState(1);
		//将对象写入数据库
		return backstage_record_mapper.insertSelective(backstageRecord)>0?backstageRecord:null;
	}

	/*
	 * (查询所有有效的后台记录)
	 * @see com.dream.service.able.All_Service_able#list()
	 */
	@Override
	public List<BackstageRecord> list() {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		BackstageRecordExample backstageRecordExample = new BackstageRecordExample();
		//设置排序规则
		backstageRecordExample.setOrderByClause("bid desc");
		//创建查询条件工具内部类
		BackstageRecordExample.Criteria criteria = backstageRecordExample.createCriteria();
		//设置查询条件,state=1
		criteria.andStateEqualTo(1);
		//返回查询到的数据集
		return backstage_record_mapper.selectByExample(backstageRecordExample);
	}

	@Override
	public BackstageRecord getT(String... str) {
		// TODO Auto-generated method stub
		return null;
	}
	/*
	 * (查询指定条数和位置的数据,传入开始的位置，和返回的数据数目)
	 * @see com.dream.service.able.Limit_able#list_Limit(java.lang.String[])
	 */
	@Override
	public List<BackstageRecord> list_Limit(String... str) {
		//切换数据库，从库
		DBContextHolder.setDbType(DBContextHolder.slave);
		//创建查询工具类
		BackstageRecordExample backstageRecordExample = new BackstageRecordExample();
		//设置排序规则
		backstageRecordExample.setOrderByClause("bid desc");
		//设置查询的开始位置
		backstageRecordExample.setOffset(Integer.parseInt(str[0]));
		//设置查询返回的数目
		backstageRecordExample.setLimit(Integer.parseInt(str[1]));
		//创建查询条件工具内部类
		BackstageRecordExample.Criteria criteria = backstageRecordExample.createCriteria();
		//设置查询条件,state=1
		criteria.andStateEqualTo(1);
		//返回查询到的数据集
		return backstage_record_mapper.selectByExample(backstageRecordExample);
	}

}
